package com.ruoyi.forApp.controller;


import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.forApp.domain.LeaveForApp;
import com.ruoyi.framework.shiro.service.LoginService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.project.system.course.domain.CourseArrange;
import com.ruoyi.project.system.course.mapper.CourseArrangeMapper;
import com.ruoyi.project.system.leave.domain.Leave;
import com.ruoyi.project.system.leave.service.ILeaveService;
import com.ruoyi.project.system.user.domain.User;
import com.ruoyi.project.system.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//安卓端登陆认证(无验证码)
@Controller
public class LeaveControllerForApp extends BaseController {
    @Autowired
    private ILeaveService leaveService;

    @Autowired
    private IUserService userService;

    @Autowired
    private CourseArrangeMapper courseArrangeMapper;

    @PostMapping("/getLeaveInfo")
    @ResponseBody
    public Map<String,Object> getLeaveInfo(Long userId)
    {
        Map<String,Object> map=new HashMap<>();
        Leave leave1=new Leave();
        leave1.setStudentId(userId);
        List<LeaveForApp> leaveList=leaveService.selectLeaveListForApp(leave1);
        if (leaveList.size()>0){
            map.put("success",true);
            map.put("leaveList",leaveList);
        }else {
            map.put("success",false);
        }
        return map;
    }
    @PostMapping("/saveLeaveInfo")
    @ResponseBody
    public Map<String,Object> saveLeaveInfo(Long userId,String ope,Long leaveId,Long courseId,String reason,String leaveTime)
    {
        Map<String,Object> map=new HashMap<>();
        Leave leave1=new Leave();
        User user=userService.selectUserById(userId);
        leave1.setStudentId(userId);
        leave1.setCourseId(courseId);
        leave1.setReason(reason);
        leave1.setLeaveTime(DateUtils.parseDate(leaveTime));
        leave1.setDeptId(user.getDeptId());
        CourseArrange courseArrange=new CourseArrange();
        courseArrange.setDeptId(leave1.getDeptId());
        courseArrange.setCourseId(courseId);
        List<CourseArrange> courseArranges=courseArrangeMapper.selectCourseArrangeList(courseArrange);
        leave1.setApproverId(courseArranges.get(0).getTeacherId());
        int i=0;
        if (ope.equals("0")){
            if (leave1.getCourseId()==-1){
                map.put("success",false);
                map.put("msg","错误的courseId");
                return map;
            }
            i=leaveService.insertLeave(leave1);
            if (i>0){
                map.put("success",true);
                map.put("msg","新增成功");
            }else {
                map.put("success",false);
                map.put("msg","新增失败");
            }

        }else {
            leave1.setLeaveId(leaveId);
            i=leaveService.updateLeave(leave1);
            if (leave1.getCourseId()==-1){
                map.put("success",false);
                map.put("msg","错误的courseId");
                return map;
            }
            if (i>0){
                map.put("success",true);
                map.put("msg","保存成功");
            }else {
                map.put("success",false);
                map.put("msg","保存失败");
            }
        }

        return map;
    }
}